package com.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.entity.Admin;
import com.health.entity.pojo.dto.AdminDTO;
import com.health.entity.pojo.dto.DeleteAdminShelvesMultiDTO;
import com.health.entity.pojo.vo.AdminVO;
import com.health.entity.pojo.vo.QueryAdminVO;
import com.health.mapper.AdminMapper;
import com.health.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台管理员表 服务实现类
 * </p>
 *
 * @author sanchuan
 * @since 2023-04-14
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminMapper tm;

    private LambdaQueryWrapper<QueryAdminVO> wrapper;

    /**
     * 登陆
     * @param username
     * @return
     */
    @Override
    public Admin dologin(String username) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    /**
     * 获得管理人员信息
     * @param token
     * @return
     */
    @Override
    public AdminVO findUserInfo(String token) {
        AdminVO admin = tm.findUserInfo(token);
        return admin;
    }

    /**
     * 获得管理人员列表并分页+模糊查询
     * @param pageNum
     * @param pageSize
     * @param adminDTO
     * @return
     */
    @Override
    public IPage<QueryAdminVO> listAdminPage(Integer pageNum, Integer pageSize, AdminDTO adminDTO) {
        List<QueryAdminVO> AdminOS = adminMapper.listAdminPage(adminDTO);
        List<QueryAdminVO> list = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < AdminOS.size(); i++) {
            list.add(AdminOS.get(curIdx + i));
        }
        IPage<QueryAdminVO> iPage = new Page<>(pageNum,pageSize);
        iPage.setRecords(list);
        iPage.setTotal(AdminOS.size());
        return iPage;
    }

    /**
     * 为新增管理人员设置角色
     * @param aid
     * @param rid
     * @return
     */
    @Override
    public Boolean saveAdminRole(Integer aid, Integer rid) {
        return adminMapper.saveAdminRole(aid,rid);
    }

    /**
     * 为管理人员更新角色
     * @param aid
     * @param rid
     * @return
     */
    @Override
    public Boolean upadteAdminRole(Integer aid, Integer rid) {
        return  adminMapper.upadteAdminRole(aid,rid);
    }

    /**
     * 根据ID删除管理人员
     * @param id
     * @return
     */
    @Override
    public Boolean deleteAdminRole(Integer id,Integer rid) {
        return adminMapper.deleteAdminRole(id,rid);
    }

    /**
     * 批片删除管理人员角色
     * @param selectedData
     * @return
     */
    @Override
    public Boolean deleteAdminRoleMulti(List<DeleteAdminShelvesMultiDTO> selectedData) {
        return adminMapper.deleteAdminRoleMulti(selectedData);
    }
}
